package com.mySchool.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mySchool.entity.*;
import com.mySchool.entity.DTO.*;
import com.mySchool.entity.PO.*;
import com.mySchool.mapper.*;
import com.mySchool.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.NullValueInNestedPathException;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
* @author : Hinton
* @date : 2024/10/18 上午9:29
*/
@Service
@Slf4j
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Override
    public BookInsertResponse insert(BookInsertRequest newBook) {
        BookPO bookP = new BookPO(newBook);

        bookMapper.insert(bookP);

        return new BookInsertResponse(bookP.getId(), bookP.getName(), bookP.getGmtCreate());
    }


    @Override
    public void update(BookUpdateRequest newBook) {
        bookMapper.update(newBook);
    }

    @Override
    @Transactional
    public PageResult<BookDetailResponse> queryHigher(BookDetailQueryRequest request) {
        PageHelper.startPage(request.getPageRequest().getPageNo(), request.getPageRequest().getPageSize());

        List<BookDetailResponse> list = bookMapper.queryHigher(request.getName());
        
        // 解析查询结果, 并封装
        Page<BookDetailResponse> p = (Page<BookDetailResponse>) list;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new IllegalArgumentException("传入值为空");
        }

        bookMapper.deleteByIds(ids);
    }

}
